package cn.edu.zxj.day_2024_10_09;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * Description：
 *
 * @author: zxj
 * @date: 2024-10-19
 * @time: 18:58:30
 */
public class CatchArticleDataUtil {
    private static final String BASH_URL = "http://localhost:4000/api";
    private static final String DEFAULT_USER = "admin";
    private static final String DEFAULT_PASSWORD = "admin";

    private String postLogin() {
        String loginUrl = BASH_URL + "/login";
        StringBuilder result = new StringBuilder();
        try {
            URL url = new URL(loginUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Accept", "application/json");
            conn.setDoOutput(true);

            // Add authentication if needed
            UserInfo userInfo = new UserInfo();
            userInfo.setUsername(DEFAULT_USER);
            userInfo.setPassword(DEFAULT_PASSWORD);
            String userJson = JSON.toJSONString(userInfo);
            conn.getOutputStream().write(userJson.getBytes());


            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }
            rd.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        String ret = result.toString();
        JSONObject  parse = JSONObject.parseObject(ret);
        JSONObject o = (JSONObject) parse.get("data");
        return o.getString("token");
    }

    private List<ArticleListBean> getArticleList(String token) {
        String articleListUrl = BASH_URL + "/article?current=1&pageSize=1000";
        StringBuilder result = new StringBuilder();
        try {
            URL url = new URL(articleListUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "application/json");
            conn.setRequestProperty("Authorization", "Bearer " + token);


            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }
            rd.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject parse =(JSONObject) JSON.parse(result.toString());
        JSONArray o = (JSONArray) ((JSONObject) parse.get("data")).get("rows");
        return o.toJavaList(ArticleListBean.class);
    }

    private ArticleInfo getArticleDetail(String token, String id) {
        String articleDetailUrl = BASH_URL + "/article/" + id;
        StringBuilder result = new StringBuilder();
        try {
            URL url = new URL(articleDetailUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "application/json");
            conn.setRequestProperty("Authorization", "Bearer " + token);

            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }
            rd.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        ArticleInfo articleInfo = ((JSONObject) JSON.parse(result.toString())).getObject("data",ArticleInfo.class); ;


        return articleInfo;
    }


    public static List<ArticleInfo> getArticleList() {
        CatchArticleDataUtil catchArticleDataUtil = new CatchArticleDataUtil();
        String token = catchArticleDataUtil.postLogin();
        List<ArticleListBean> articleList = catchArticleDataUtil.getArticleList(token);
        // 获取 articleList 中的 id存储到集合中
        // articleList.stream().map(ArticleListBean::getId).forEach(System.out::println);
        List<String> collect = articleList.stream().map(new Function<ArticleListBean, String>() {
            @Override
            public String apply(ArticleListBean articleListBean) {
                return articleListBean.getId();
            }
        }).collect(Collectors.toList());

        List<ArticleInfo> articleInfos = articleList.stream().map(new Function<ArticleListBean, ArticleInfo>() {
            @Override
            public ArticleInfo apply(ArticleListBean articleListBean) {
                return catchArticleDataUtil.getArticleDetail(token, articleListBean.getId());
            }
        }).collect(Collectors.toList());

        return articleInfos;
        
    }
}
